package com.taimi.springbootzendaodemo.mail;

import java.util.function.Consumer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.taimi.springbootzendaodemo.system.EnvironmentUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class TaimiMailSender {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private EnvironmentUtils envUtils;

    public static final String SEMICOLON = ";";

    public void sendMail(MailTemplate mailTemplate) throws MessagingException {
        log.info("mailTemplate: {}", mailTemplate);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setFrom(mailTemplate.getFrom_());
        String to = mailTemplate.getTo();
        addOrSetProperty(to, t -> {
            try {
                mimeMessageHelper.addTo(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }, t -> {
            try {
                mimeMessageHelper.setTo(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });
        String cc = mailTemplate.getCc();
        addOrSetProperty(cc, t -> {
            try {
                mimeMessageHelper.addCc(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }, t -> {
            try {
                mimeMessageHelper.setCc(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });
        String bcc = mailTemplate.getBcc();
        addOrSetProperty(bcc, t -> {
            try {
                mimeMessageHelper.addBcc(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }, t -> {
            try {
                mimeMessageHelper.setBcc(t);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });

        mimeMessageHelper.setSubject(mailTemplate.getSubject());
        String text = mailTemplate.getTemplate();
        mimeMessageHelper.setText(text,true);
        if (envUtils.isTest()) {
            log.info("do not send mail in test environment");
        }
        if (envUtils.isNotTest()) {
            javaMailSender.send(mimeMessage);
            log.info("send mail completed");
        }
    }

    private void addOrSetProperty(String destination, Consumer<String> add, Consumer<String> set) {
        if (StringUtils.isEmpty(destination)) {
            return;
        }
        if (destination.contains(SEMICOLON)) {
            String[] destinations = destination.split(SEMICOLON);
            for (String des : destinations) {
                add.accept(des);
            }
        } else {
            set.accept(destination);
        }
    }
}